﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using TheOutfield.UmbExt.PrivateMessaging.Data;
using TheOutfield.UmbExt.PrivateMessaging.Model;
using TheOutfield.UmbExt.PrivateMessaging.Util;

namespace TheOutfield.UmbExt.PrivateMessaging.Services
{
    public class PrivateMessageService
    {
        protected MessageRepository _messageRepository;
        protected RecipientRepository _recipientRepository;
        protected PrivateMessageRepository _privateMessageRepository;

        public PrivateMessageService()
        {
            _messageRepository = new MessageRepository();
            _recipientRepository = new RecipientRepository();
            _privateMessageRepository = new PrivateMessageRepository();
        }

        public void SendPrivateMessage(string[] to, string subject, string body)
        {
            var member = Membership.GetUser(true);
            if (member == null)
                throw new ApplicationException("You must be logged in");

            SendPrivateMessage(Convert.ToInt32(member.ProviderUserKey), to, subject, body);
        }

        public void SendPrivateMessage(int from, string[] to, string subject, string body)
        {
            Message m = new Message();
            m.Body = body;
            m.Subject = subject;
            m.CreateDate = DateTime.Now;
            m.MemberId = from;

            int messageId = _messageRepository.CreateMessage(m).MessageId;

            //create a copy in the sent items folder for this user
            Recipient sender = new Recipient();
            sender.MessageId = messageId;
            sender.MemberId = from;
            sender.Folder = MessageFolder.Sent;
            sender.Status = MessageStatus.Read;

            _recipientRepository.CreateRecipient(sender);

            // Send to people in the "To" field
            foreach (string s in to)
            {
                var id = s.Trim();
                if (id == "")
                    continue;

                if (id.Contains("@"))
                    id = Membership.GetUserNameByEmail(id);

                var toMember = id.IsInt32() ?
                    Membership.GetUser(Convert.ToInt32(id), false) :
                    Membership.GetUser(id, false);

                if (toMember != null)
                {
                    Recipient recipient = new Recipient();
                    recipient.MessageId = messageId;
                    recipient.MemberId = Convert.ToInt32(toMember.ProviderUserKey);
                    recipient.Folder = MessageFolder.Inbox;
                    recipient.Status = MessageStatus.Unread;
                    _recipientRepository.CreateRecipient(recipient);
                }
            }
        }

        public PrivateMessage GetPrivateMessage(int messageId)
        {
            var member = Membership.GetUser(true);
            if (member == null)
                throw new ApplicationException("You must be logged in");

            return _privateMessageRepository.GetPrivateMessage(Convert.ToInt32(member.ProviderUserKey),
                messageId);
        }

        public List<PrivateMessage> GetPrivateMessages(MessageFolder messageFolder)
        {
            var member = Membership.GetUser(true);
            if (member == null)
                throw new ApplicationException("You must be logged in");

            return _privateMessageRepository.GetPrivateMessages(Convert.ToInt32(member.ProviderUserKey),
                messageFolder);
        }

        public void UpdatePrivateMessageStatus(int messageId, MessageStatus status)
        {
            var pm = GetPrivateMessage(messageId);
            pm.Recipient.Status = status;

            _recipientRepository.UpdateRecipient(pm.Recipient);
        }

        public void UpdatePrivateMessageFolder(int messageId, MessageFolder folder)
        {
            var pm = GetPrivateMessage(messageId);
            pm.Recipient.Folder = folder;

            _recipientRepository.UpdateRecipient(pm.Recipient);
        }

        public void DeletePrivateMessage(int messageId)
        {
            var member = Membership.GetUser(true);
            if (member == null)
                throw new ApplicationException("You must be logged in");

            _privateMessageRepository.DeletePrivateMessage(Convert.ToInt32(member.ProviderUserKey),
                messageId);
        }
    }
}